Outdated CHICKEN release
This is a manual page for an old and unsupported version of CHICKEN. If you are still using it, please consider migrating to the latest version. You can find the manual for the latest release here.
Unit regex
This library unit provides support for regular expressions. The regular expression package used is irregex written by Alex Shinn. See http://synthcode.com/scheme/irregex/ for information about the particular regexp flavor and extensions provided by this library. Also see SRE regular-expression notation.
To test that irregex support has been built into Chicken properly, try:
(require 'regex) (feature? 'irregex) => #t
Note on SRE syntax: instead of (| <sre> ...), use (or <sre> ...). The character | (vertical pipe) is handled specially in Chicken, and cannot be used on its own as a symbol.
grep
[procedure] (grep REGEX LIST)
Returns all items of LIST that match the regular expression REGEX. This procedure could be defined as follows:
(define (grep regex lst)
(filter (lambda (x) (string-search regex x)) lst) )
glob->regexp
[procedure] (glob->regexp PATTERN)
Converts the file-pattern PATTERN into a regular expression.
(glob->regexp "foo.*") => "foo\..*"
PATTERN should follow "glob" syntax. Allowed wildcards are
* [C...] [C1-C2] [-C...] ?
glob?
[procedure] (glob? STRING)
Does the STRING have any "glob" wildcards?
A string without any "glob" wildcards does not meet the criteria, even though it technically is a valid "glob" file-pattern.
regexp
[procedure] (regexp STRING [IGNORECASE [IGNORESPACE [UTF8]]])
Returns a precompiled regular expression object for string. The optional arguments IGNORECASE, IGNORESPACE and UTF8 specify whether the regular expression should be matched with case- or whitespace-differences ignored, or whether the string should be treated as containing UTF-8 encoded characters, respectively.
regexp?
[procedure] (regexp? X)
Returns #t if X is a precompiled regular expression, or #f otherwise.
string-match
string-match-positions
[procedure] (string-match REGEXP STRING ) [procedure] (string-match-positions REGEXP STRING )
Matches the regular expression in REGEXP (a string or a precompiled regular expression) with STRING and returns either #f if the match failed, or a list of matching groups, where the first element is the complete match. For each matching group the result-list contains either: #f for a non-matching but optional group; a list of start- and end-position of the match in STRING (in the case of string-match-positions); or the matching substring (in the case of string-match). Note that the exact string is matched. For searching a pattern inside a string, see below. Note also that string-match is implemented by calling string-search with the regular expression wrapped in ^ ... $. If invoked with a precompiled regular expression argument (by using regexp), string-match is identical to string-search.
string-search
string-search-positions
[procedure] (string-search REGEXP STRING [START [RANGE]]) [procedure] (string-search-positions REGEXP STRING [START [RANGE]])
Searches for the first match of the regular expression in REGEXP with STRING. The search can be limited to RANGE characters.
string-split-fields
[procedure] (string-split-fields REGEXP STRING [MODE [START]])
Splits STRING into a list of fields according to MODE, where MODE can be the keyword #:infix (REGEXP matches field separator), the keyword #:suffix (REGEXP matches field terminator) or #t (REGEXP matches field), which is the default.
(define s "this is a string 1, 2, 3,") (string-split-fields "[^ ]+" s) => ("this" "is" "a" "string" "1," "2," "3,") (string-split-fields " " s #:infix) => ("this" "is" "a" "string" "1," "2," "3,") (string-split-fields "," s #:suffix) => ("this is a string 1" " 2" " 3")
string-substitute
[procedure] (string-substitute REGEXP SUBST STRING [MODE])
Searches substrings in STRING that match REGEXP and substitutes them with the string SUBST. The substitution can contain references to subexpressions in REGEXP with the \NUM notation, where NUM refers to the NUMth parenthesized expression. The optional argument MODE defaults to 1 and specifies the number of the match to be substituted. Any non-numeric index specifies that all matches are to be substituted.
(string-substitute "([0-9]+) (eggs|chicks)" "\\2 (\\1)" "99 eggs or 99 chicks" 2) => "99 eggs or chicks (99)"
Note that a regular expression that matches an empty string will signal an error.
string-substitute*
[procedure] (string-substitute* STRING SMAP [MODE])
Substitutes elements of STRING with string-substitute according to SMAP. SMAP should be an association-list where each element of the list is a pair of the form (MATCH . REPLACEMENT). Every occurrence of the regular expression MATCH in STRING will be replaced by the string REPLACEMENT
(string-substitute* "<h1>Hello, world!</h1>" '(("<[/A-Za-z0-9]+>" . ""))) => "Hello, world!"
regexp-escape
[procedure] (regexp-escape STRING)
Escapes all special characters in STRING with \, so that the string can be embedded into a regular expression.
(regexp-escape "^[0-9]+:.*$") => "\\^\\[0-9\\]\\+:.\n.\\*\\$"
Previous: Unit match
Next: Unit srfi-18